<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Http\JsonResponse;
use App\Models\Cate;
use App\Models\Comment;
use App\Models\CommentLabel;
use App\Models\Order;
use App\Models\Product;
use App\Models\ProductCate;
use App\Models\ProductTag;
use App\Services\ForgeryService;
use App\Services\VehicleService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;

class ProductController extends Controller
{
    public function index(Request $request){

        $product = Product::where('status',1)->where('cate_id',30);
        $cate = Cate::find(30);

        $product = $product->orderBy('sort','desc')->get();

        $path = 'product';
        return template('product.index',compact('product','cate','path'));
    }

    public function pods(Request $request){

        $product = Product::where('status',1)->where('cate_id',31);
        $cate = Cate::find(31);

        $product = $product->orderBy('sort','desc')->get();

        $path = 'pods';
        return template('product.index',compact('product','cate','path'));
    }

    public function host(Request $request){

        $product = Product::where('status',1)->where('cate_id',32);
        $cate = Cate::find(32);

        $product = $product->orderBy('sort','desc')->get();

        $path = 'host';
        return template('product.index',compact('product','cate','path'));
    }

    public function show($id){

        $product = Product::where('id',$id)->where('status',1)->first();
        if(!$product){
            abort(404);
        }
        $path = '';
        if($product->cate_id == 30){
            $path = 'product';
        }elseif($product->cate_id == 31){
            $path = 'pods';
        }elseif($product->cate_id == 32){
            $path = 'host';
        }else{
            abort(404);
        }

        if(!request()->is($path.'/'.$id)){
            return Redirect::to($path.'/'.$id,301);
        }


        $recommend = Product::inRandomOrder()->where('status',1)->limit(4)->get();

        $goods_delivery_untreated = app('cache.config')->get('goods_delivery');
        $goods_delivery = [];
        if($goods_delivery_untreated){
            $goods_delivery = json_decode($goods_delivery_untreated,true);
        }

        $goods_payment_untreated = app('cache.config')->get('goods_payment');
        $goods_payment = [];
        if($goods_payment_untreated){
            $goods_payment = json_decode($goods_payment_untreated,true);
        }

        /*$goods_comments_untreated = app('cache.config')->get('goods_comments');
        $goods_comments = [];
        if($goods_comments_untreated){
            $goods_comments = json_decode($goods_comments_untreated,true);
        }*/

        $comments = Comment::with('product')->where('cate_id',$product->cate_id)->orderBy('time','desc')->get();

        $comment_labels = CommentLabel::where('cate_id',$product->cate_id)->orderBy('sort')->get();

        return template('product.show',compact('product','recommend','goods_delivery','goods_payment','comments','comment_labels'));
    }

    public function comment($id,Request $request){

        $validator = Validator::make($request->all(), [
            'number' => 'required',
            'content'=> 'required',
            'star'=>'required',
        ],[
            'number.required'=>'請填寫訂單編號',
            'number.exists'=>'訂單編號不存在',
            'content.required'=>'評論内容不能爲空',
            'star.required'=>'星星不能爲空',
        ]);

        if ($validator->fails()) {
            return JsonResponse::make()->status(false)->statusCode(422)->message($validator->errors()->first())->send();
        }

        $count = Comment::where('ip',VehicleService::IP())->whereBetWeen('created_at',[Carbon::now()->startOfDay(),Carbon::now()->endOfDay()])->count();

        if($count > 5){
            return JsonResponse::make()->status(false)->statusCode(400)->message('當日評論次數過多')->send();
        }

        $order = Order::where('no',$request->number)->first();
        if(!$order){
            return JsonResponse::make()->status(false)->statusCode(400)->message('訂單號碼不存在')->send();
        }

        try {
            Comment::create([
                'cate_id'=>$id,
                'order_no'=>$request->number,
                'phone'=>$order->phone,
                'star'=>$request->star,
                'content'=>$request->get('content'),
                'status'=>0,
                'ip'=>VehicleService::IP(),
                'user_agent'=>VehicleService::userAgent(),
            ]);

            return JsonResponse::make()->message('評論成功')->send();
        }catch (\Exception $exception){
            return JsonResponse::make()->status(false)->statusCode(400)->message('評論系統異常')->send();
        }

    }

    public function commentUp(Request $request){
        $comment = Comment::find($request->id);
        if($request->like == 1){
            $comment->up = $comment->up+1;
        }else{
            $comment->up = $comment->up-1;
        }
        if($comment->up < 0){
            $comment->up = 0;
        }

        $comment->save();
    }
}
